/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.ui.filter;

import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.sinai.mshab.client.ui.Key;
import com.sinai.mshab.client.ui.aggregate.FormData;
import com.sinai.mshab.client.ui.aggregate.HorizontalComponentList;
import com.sinai.mshab.client.ui.aggregate.TableComponent;

/**
 * This is a full filtered table, allowing adding of filter aggregates and
 * filters, allowing sorting by criteria, and showing certain number of records
 * per page, as well as page navigation
 * 
 * @author serge
 * 
 */
public class AdvancedFilteredTable extends FilteredTable {

	public AdvancedFilteredTable() {

		fieldsToCustomFilters = new HashMap();

		table = new TableComponent(true, historyChangePrefix);
		table.setStyleName("sortableTable");

		// go through the keys and add columns.
		Key key = FormData.getKey();
		String table_name = key.getTableName();
		Iterator it = key.getKeys().keySet().iterator();

		// walking through the cols of the key and adding them as cols to the
		// table.
		while (it.hasNext()) {
			String col_name = (String) it.next();
			column_names.add(table_name + "." + col_name);
		}

		// another FilterAggregate button
		Button add_FilterAggregate_button = null;
		add_FilterAggregate_button = new Button("Add a Category");
		add_FilterAggregate_button.setStyleName("button");
		add_FilterAggregate_button
				.addClickListener(new AddFilterAggregateListener());

		// add another category box
		FlexTable anotherCategoryBox = new FlexTable();
		Label anotherCategoryLabel = new Label(
				"Click to add a filter Category ...");
		anotherCategoryLabel.setStyleName("important_blue_small_clickable");
		anotherCategoryLabel.addClickListener(new AddFilterAggregateListener());
		anotherCategoryBox.setWidget(0, 0, anotherCategoryLabel);
		anotherCategoryBox.setStyleName("lightshadebox");

		// populate button
		Button populate_button = new Button("Run Report");
		populate_button.setStyleName("button");
		populate_button.addClickListener(new UpdateListener());

		// second populate button
		Button populate_button2 = new Button("Run Report");
		populate_button2.setStyleName("button");
		populate_button2.addClickListener(new UpdateListener());

		// creating the sort by field.
		Label sortBylabel = new Label("Sort By:");
		sortBylabel.setStyleName("sub_description_label");
		sort_by = new ListBox();
		sort_by.addFocusListener(this);

		// creating the sort order field.
		Label sortOrderlabel = new Label("Sort Order:");
		sortOrderlabel.setStyleName("sub_description_label");
		sort_asc_desc = new ListBox();
		sort_asc_desc.addItem("Ascending");
		sort_asc_desc.addItem("Descending");
		sort_asc_desc.addFocusListener(this);

		// creating records per page dropdown
		Label recordsPerPagelabel = new Label("Show Records:");
		recordsPerPagelabel.setStyleName("sub_description_label");
		records_per_page = new ListBox();
		records_per_page.addItem("2");
		records_per_page.addItem("5");
		records_per_page.addItem("10");
		records_per_page.addItem("25");
		records_per_page.addItem("50");
		records_per_page.addItem("100");
		records_per_page.setSelectedIndex(4);
		records_per_page.addFocusListener(this);

		// the hpanel for sort by
		FlexTable sortby_panel = new FlexTable();
		sortby_panel.setWidget(0, 0, sortBylabel);
		sortby_panel.setWidget(0, 1, sort_by);
		sortby_panel.setWidget(1, 0, sortOrderlabel);
		sortby_panel.setWidget(1, 1, sort_asc_desc);
		sortby_panel.setWidget(2, 0, recordsPerPagelabel);
		sortby_panel.setWidget(2, 1, records_per_page);
		sortby_panel.setWidget(3, 1, populate_button2);
		sortby_panel.setStyleName("lightshadebox");

		// the showing records label, dont need it for now
		showing_records = new Label();
		showing_records.setStyleName("important_blue");

		// nav panel for records
		record_navigation = new HorizontalPanel();

		// adding it all.
		add(anotherCategoryBox);
		add(sortby_panel);
		add(showing_records);
		add(table);
		add(record_navigation);
	}
}
